package com.itboye.jinpuyiyuan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.bean.OptionMenuBean;
import com.itboye.jinpuyiyuan.bean.TestServerOptionBean;
import com.itboye.jinpuyiyuan.interfaces.IGoodsCardRA;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2018/2/5 0005.
 */

public class ShopCartAdapter extends BaseAdapter {
    private ArrayList<OptionMenuBean.Depts.Items> list;
    private IGoodsCardRA goodsCard;

    private Context context;
    private LayoutInflater inflater;
    private int clickPosition;
    private String leftId;
    private int viewType;
    String deptId;
    int positions;
    /**
     *
     * @param context
     * @param goodsCard
     * @param list
     */
    public  ShopCartAdapter(Context context,IGoodsCardRA goodsCard,ArrayList<OptionMenuBean.Depts.Items> list){
        this.list=list;
        this.goodsCard=goodsCard;
        this.context=context;
        this.deptId=deptId;
        this.positions=positions;
        inflater=LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       final MyViewHolder myViewHolder;
        if (convertView==null){
            myViewHolder=new MyViewHolder();
//            if (viewType==1){
//                convertView=inflater.inflate(R.layout.item_testserver_optionrightlist,parent,false);
//            }else if (viewType==2){
                convertView=inflater.inflate(R.layout.item_shopcart,parent,false);
//            }

            myViewHolder.name=convertView.findViewById(R.id.name);
            myViewHolder.price=convertView.findViewById(R.id.price);
            myViewHolder.add=convertView.findViewById(R.id.add);
            myViewHolder.delete=convertView.findViewById(R.id.delete);
            myViewHolder.numTv=convertView.findViewById(R.id.numTv);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        myViewHolder.name.setText(list.get(position).getItemname());
        myViewHolder.price.setText("¥"+list.get(position).getItemprice());
        myViewHolder.numTv.setText(list.get(position).getSelects()+"");


        if ("0".equals(myViewHolder.numTv.getText().toString())||"".equals(myViewHolder.numTv.getText().toString())){
            myViewHolder.numTv.setVisibility(View.GONE);
            myViewHolder.delete.setVisibility(View.GONE);
            myViewHolder.add.setVisibility(View.VISIBLE);
        }else {
            myViewHolder.numTv.setVisibility(View.VISIBLE);
            myViewHolder.delete.setVisibility(View.VISIBLE);
            myViewHolder.add.setVisibility(View.INVISIBLE);
        }

        myViewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                myViewHolder.numTv.setVisibility(View.VISIBLE);
                myViewHolder.delete.setVisibility(View.VISIBLE);

                int integer = Integer.valueOf(myViewHolder.numTv.getText().toString());
                myViewHolder.numTv.setText(integer+1+"");
                if (myViewHolder.numTv.getText().toString().equals("1")){
                    myViewHolder.add.setVisibility(View.INVISIBLE);
                }else {
                    myViewHolder.add.setVisibility(View.VISIBLE);
                }
                Log.d("add===",list.get(position).getDept().trim());
//                list.get(position).setSelects(integer+1);
                goodsCard.onGoodsChange(list.get(position).getDept().trim(),list.get(position).getItemid(),true);

               ShopCartAdapter.this.notifyDataSetChanged();
            }
        });
        myViewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int integer = Integer.valueOf(myViewHolder.numTv.getText().toString());
                if ("1".equals(myViewHolder.numTv.getText().toString())){
                    myViewHolder.numTv.setVisibility(View.GONE);
                    myViewHolder.delete.setVisibility(View.GONE);
                }
                myViewHolder.numTv.setText(integer-1+"");

                if (myViewHolder.numTv.getText().toString().equals("0")){
                    myViewHolder.add.setVisibility(View.VISIBLE);
                }else {
                    myViewHolder.delete.setVisibility(View.VISIBLE);
                }
                goodsCard.onGoodsChange(list.get(position).getDept().trim(),list.get(position).getItemid(),false);

                ShopCartAdapter.this.notifyDataSetChanged();
            }
        });
        return convertView;
    }

    //添加right数据和购物车显示相关的view
    public void addData(ArrayList<OptionMenuBean.Depts.Items> list){
        this.list=list;
        this.notifyDataSetChanged();
    }

    class MyViewHolder{
        TextView name,price,numTv;
        ImageView add,delete;

    }

}
